package zjc.bookkeeping.tools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import com.github.mikephil.charting.renderer.LineRadarRenderer;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import zjc.bookkeeping.R;
import zjc.bookkeeping.activity.MainActivity;
import zjc.bookkeeping.db.ConsumptionBean;

public class ToolsClass {
    //字符串数组查询内容下标
    public static int getStringIndex(String str, String[] names) {
        for (int i=0;i<names.length;i++){
            if (names[i].contains(str)){
//                Log.i("下标","是   "+i);
                return i;
            }
        }
        return -1;
    }
    //等待Dialog
    public static void showWaitingDialog(Activity activity,String titleStr,String contentStr) {
        ProgressDialog waitingDialog = new ProgressDialog(activity);
        waitingDialog.setTitle(titleStr);
        waitingDialog.setMessage(contentStr);
        waitingDialog.setIndeterminate(true);
        waitingDialog.setCancelable(false);
        waitingDialog.show();
    }

    //    动态申请储存权限
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };

    public static void verifyStoragePermissions(Activity activity) {

        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//    提示显示
    public static void showToast(Context context, String str, int showTime) {
        Toast toast = Toast.makeText(context,str,showTime);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }
    //获取时间，0为现在时间，+n为未来n天，-n为过去n天
    public static String getYMDTime(int day) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE) + day);
        String timeStr = simpleDateFormat.format(calendar.getTime());
        return timeStr;
    }
    //获取时间，0为现在时间，+n为未来n天，-n为过去n天
    public static String getYMTime(int day) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE) + day);
        String timeStr = simpleDateFormat.format(calendar.getTime());
        return timeStr;
    }
//    图片转为二进制数据
    public static byte[] bitmapToByte(Context context,int id){
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),id);
        int size = bitmap.getWidth() * bitmap.getHeight() * 4;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(size);
        try{
            bitmap.compress(Bitmap.CompressFormat.PNG,100,byteArrayOutputStream);
            byte[] imageData = byteArrayOutputStream.toByteArray();
            return imageData;
        }catch (Exception e){
        }finally {
            try {
                bitmap.recycle();
                byteArrayOutputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return new byte[0];
    }
//    根据图片名获取图片资源id
    public static int getResourceId(String imageName){
        Field field = null;
        try {
            field = R.drawable.class.getDeclaredField(imageName);
            int resource_id = field.getInt(field.getName());
            return resource_id;
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
